/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.dao.student;

import java.sql.ResultSet;
import java.sql.SQLException;

import edu.uwm.universitydb.encryption.JavaEncryption;
import edu.uwm.universitydb.encryption.MissingKeyException;
import edu.uwm.universitydb.framework.nontransactional.db.command.impl.NonTransactionalDBFetchCommandBase;
import edu.uwm.universitydb.model.Department;
import edu.uwm.universitydb.model.Student;
import edu.uwm.universitydb.service.ServiceFactory;
import edu.uwm.universitydb.service.dept.DepartmentService;

/**
 * <code>SearchStudentCommandBase</type>
 * 
 * Base command class for searching students
 * 
 * @author Shyam Nair
 */
public abstract class SearchStudentCommandBase<T> extends NonTransactionalDBFetchCommandBase<T>
{
    /** static attribute representing column ssn */
    private static final String TUPLE_COLUMN_SSN = "s.grad_ssn";

    /** static attribute representing column name */
    private static final String TUPLE_COLUMN_NAME = "name";

    /** static attribute representing column age */
    private static final String TUPLE_COLUMN_AGE = "age";

    /** static attribute representing column rank */
    private static final String TUPLE_COLUMN_PROGRAM = "program";

    /** static attribute representing column research_speciality */
    private static final String TUPLE_COLUMN_DEPT = "dno";

    /** base select portion of sql */
    protected static final String SELECT_SQL = "select " + TUPLE_COLUMN_SSN + ", "
        + TUPLE_COLUMN_NAME + ", " + TUPLE_COLUMN_AGE + ", " + TUPLE_COLUMN_PROGRAM + ", "
        + TUPLE_COLUMN_DEPT + " from university.grad_student s";

    /**
     * <code>getStudent(...)</code> parses one row of the result set to create a professor object
     * 
     * @param result
     * @return
     * @throws SQLException
     */
    protected Student getStudent(final ResultSet result) throws SQLException
    {
        // process one tuple
        final String ssn = result.getString(TUPLE_COLUMN_SSN);
        String age = result.getString(TUPLE_COLUMN_AGE);
		try {
	        JavaEncryption jce = JavaEncryption.getInstance();
			age = jce.decrypt(age);
		} catch (MissingKeyException e) {
			//Key not found. age is encrypted data
		}
        final String name = result.getString(TUPLE_COLUMN_NAME);
        final String program = result.getString(TUPLE_COLUMN_PROGRAM);
        final Integer deptNum = result.getInt(TUPLE_COLUMN_DEPT);
        DepartmentService deptService = ServiceFactory.getInstance().getDepartmentService();
        final Department dept = deptService.getDepartmentByID(deptNum);
        // return the professor
        return new Student(ssn, age, name, program, dept);
    }
}
/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 19, 2014   Shyam Nair
 * File created
 * ----------------------------------------------------------------------------
 */